﻿// Projectile.cs

//Using declarations
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Midgard
{
    class Projectile
    {
        public Texture2D texture; // Image representing the Projectile

        // Position of the Projectile relative to the upper left side of the screen
        public Vector2 position;

        // State of the Projectile
        public bool active;

        // The amount of damage the projectile can inflict to an enemy
        public int damage;

        // Represents the viewable boundary of the game
        Rectangle viewport;

        //Used to rotate the star
        float rotation;
        float curRot;
        // Get the width of the projectile ship
        public int Width
        {
            get { return texture.Width; }
        }

        // Get the height of the projectile ship
        public int Height
        {
            get { return texture.Height; }
        }

        // Determines how fast the projectile moves
        Vector2 velocity;

        Vector2 acceleration;

        // Factions are used to differentiate projectiles so enemies don't kill each other, etc
        Faction faction;

        public Faction ProjFaction
        {
            get { return faction; }
        }
        public Rectangle boundRect
        {
            get { return new Rectangle((int)position.X, (int)position.Y, Width, Height); }
        }
        public enum Faction
        { player, enemy };

        public Projectile(Texture2D ProjectileSpriteTexture, Vector2 ProjectilePosition, Vector2 ProjectileVelocity,
            Rectangle ViewportRectangle, int ProjectileDamage, float ProjectileRotation, Faction ProjectileFaction)
        {
            texture = ProjectileSpriteTexture;
            position = ProjectilePosition;
            velocity = ProjectileVelocity;
            viewport = ViewportRectangle;
            damage = ProjectileDamage;
            rotation = ProjectileRotation;
            faction = ProjectileFaction;
            acceleration = new Vector2(0, 0);
            /* Initializers */
            curRot = 0;
            Initialize();
        }

        public Projectile(Texture2D ProjectileSpriteTexture, Vector2 ProjectilePosition, Vector2 ProjectileVelocity, Vector2 ProjectileAcceleration,
            Rectangle ViewportRectangle, int ProjectileDamage, float ProjectileRotation, Faction ProjectileFaction)
        {
            texture = ProjectileSpriteTexture;
            position = ProjectilePosition;
            velocity = ProjectileVelocity;
            viewport = ViewportRectangle;
            damage = ProjectileDamage;
            rotation = ProjectileRotation;
            faction = ProjectileFaction;
            acceleration = ProjectileAcceleration;
            curRot = 0;
            /* Initializers */
            Initialize();
        }

        public void Initialize()
        {
            active = true;
            position.X += texture.Width / 2f;
            //position.Y += texture.Height / 2f;
        }


        public void Update()
        {
            position.X += velocity.X;
            position.Y += velocity.Y;
            curRot += rotation;
            velocity.X += acceleration.X;
            velocity.Y += acceleration.Y;
            // Deactivate the projectile if it goes out of screen.
            if (position.Y + Height < 0 || position.Y + Height > viewport.Height - 40 ||
                position.X < -10 || position.X> viewport.Width + 10)
            {
                active = false;
                //Console.WriteLine((position.X - 259) + " posX-259");
                //Console.WriteLine((position.X - viewport.Width) + "posX - viewWidth");
                //Console.WriteLine((position.X - 259 - viewport.Width) + "posX-259-viewWidth");
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(texture, new Rectangle(position.X, position.Y, Width, Height), Color.White, rotation, new Vector2(), SpriteEffects.None, 0f);
            //spriteBatch.Draw(textures[curTexture], position, null, curFade, 0f, new Vector2(), 1, SpriteEffects.FlipHorizontally, 0);
            spriteBatch.Draw(texture, position, null, Color.White, curRot, new Vector2(texture.Width/2f,texture.Height/2f),1,SpriteEffects.None,0f);
            //Console.WriteLine("Position: " + position.ToString());
        }

    }
}
